
//
//  HTComposePictrueView.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/27.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit
import SVProgressHUD
let  composePictureIdent = "composePicturecell"

var RandomColor: UIColor {
    return UIColor(red: CGFloat(random()%256)/255, green: CGFloat(random()%256)/255, blue: CGFloat(random()%256)/255, alpha: 1)
}


class HTComposePictrueView: UICollectionView {

    lazy var imagess : [UIImage] = []
    
    //选择图片调用的必报
    var  selectedPictrueClose : (() -> ())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
    
        
        
        
        backgroundColor = UIColor.yellowColor()
        
        registerClass(HTComposePivtureControllerCell.self, forCellWithReuseIdentifier: composePictureIdent)
        
        
        dataSource = self
        delegate = self
    }
    
    func images(images : UIImage){
        
        if imagess.count < 9 {
        
            imagess.append(images)
            
            reloadData()
        }else{
        
            SVProgressHUD.showErrorWithStatus("你傻逼吗?没看已经满了")
        }
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let  margin : CGFloat = 5
        let itemH = (self.frame.width - 2 * margin) / 3
        let  layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: itemH, height: itemH)
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
        
    }
    
}


extension HTComposePictrueView : UICollectionViewDataSource,UICollectionViewDelegate{


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.hidden = imagess.count == 0
        return imagess.count + ((imagess.count == 0 || imagess.count == 9) ? 0 : 1)
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let  cell = collectionView.dequeueReusableCellWithReuseIdentifier(composePictureIdent, forIndexPath: indexPath) as! HTComposePivtureControllerCell
        
        if indexPath.item == imagess.count{
            
//            cell.image = UIImage(named: "compose_pic_add")
            cell.image = nil
        
        }else{
        
            cell.image = imagess[indexPath.row]
            
        }
        
        cell.deleteButtonClose = {[weak self] in
            
            self?.imagess.removeAtIndex(indexPath.item)
            self?.reloadData()
        }
        
        
        return cell
    }
    
 /// 监听cell点击
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        
        if indexPath.item == imagess.count{
            
            selectedPictrueClose?()
        
        }
    }
    
    
    
}

class HTComposePivtureControllerCell : UICollectionViewCell{

    
    var deleteButtonClose: (() -> ())?
    
    var image : UIImage?{
    
        didSet{
        
            if image == nil{
                imageView.image = UIImage(named: "compose_pic_add")
                deleteButton.hidden = true
                
            }else {
            
                deleteButton.hidden = false
                imageView.image = image
            }
            
            

        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
  

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        contentView.addSubview(imageView)
        contentView.addSubview(deleteButton)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    
    @objc private func deleteButtonClick(){
    
        deleteButtonClose?()
    }
    
    
    private lazy var imageView : UIImageView = UIImageView()
    
    private lazy var deleteButton : UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        return button
    }()
    
}
